document.addEventListener('DOMContentLoaded', function() {
    const logContent = document.querySelector('.log-content');
    const logCount = document.querySelector('.log-count');
    const downloadBtn = document.querySelector('.download-btn');
    const clearBtn = document.querySelector('.clear-btn');

    // 加载日志内容
    function loadLogs() {
        fetch('/api_logs')
            .then(response => response.json())
            .then(data => {
                logContent.textContent = data.content;
                logCount.textContent = `当前日志条数：${data.lineCount}`;
            })
            .catch(error => console.error('Error loading logs:', error));
    }

    // 下载日志
    downloadBtn.addEventListener('click', function() {
        window.location.href = '/download_api_log';
    });

    // 清空日志
    clearBtn.addEventListener('click', function() {
        fetch('/clear_api_log', { method: 'POST' })
            .then(response => response.json())
            .then(data => {
                if (data.message === '日志已清空并备份') {
                    loadLogs();
                }
            })
            .catch(error => console.error('Error clearing logs:', error));
    });

    // 自动刷新日志
    setInterval(loadLogs, 5000);

    // 初始加载日志
    loadLogs();
});